package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Attractions;
import cn.wolfcode.luowowo.article.domain.AttractionsDetail;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.query.AttractionsQuery;
import cn.wolfcode.luowowo.article.service.IAttractionsDetailService;
import cn.wolfcode.luowowo.article.service.IAttractionsService;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.cache.service.IAttractionStatisRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.AttractionsStatisVO;
import cn.wolfcode.luowowo.comment.domain.AttractionsComment;
import cn.wolfcode.luowowo.comment.query.AttractionsCommentQuery;
import cn.wolfcode.luowowo.comment.service.IAttractionsCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("attractions")
public class AttractionsController {

    @Reference
    private IAttractionsService attractionsService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IAttractionsDetailService attractionsDetailService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private IAttractionsCommentService attractionsCommentService;
    @Reference
    private IAttractionStatisRedisService attractionStatisRedisService;


    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") AttractionsQuery qo, Long CharacteristicId, Long destId) {
        //获取排行前五的景点
        List<Attractions> list = attractionsService.Top5(destId);
        //获取热门景点10个
        List<Attractions> hotList = attractionsService.hotList(destId);
        //吐司
        ArrayList<Destination> toasts = new ArrayList<>();
        getToasts(destId, toasts);
        Collections.reverse(toasts);
        toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        Destination dest = destinationService.selecOne(destId);
        model.addAttribute("dest", dest);

        qo.setPageSize(5);
        Attractions attractions = new Attractions();
        attractions.setList(hotList);

        model.addAttribute("list", list);
        model.addAttribute("attractionsList", attractions);

        PageInfo pageInfo = attractionsService.pageResult(qo, destId);
        model.addAttribute("pageInfo", pageInfo);
        if (CharacteristicId != null) {
            List<Attractions> pageInfoList = pageInfo.getList();
            if (pageInfoList.size() > 0) {
                Long characteristicId = pageInfoList.get(0).getCharacteristicId();
                model.addAttribute("CharacteristicId", characteristicId);
            }

        }

        return "attractions/list";
    }

    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") AttractionsCommentQuery qo) {
        //去查询所有页面需要的数据.然后转到页面模板那里再返会给前端
        //查询页面所有的数据
        Page<AttractionsComment> page = attractionsCommentService.selectCommentForList(qo);
        model.addAttribute("page", page);

        return "strategy/AttractioncommentTpl";
    }

    /**
     * 内部方法用来迭代吐丝的
     *
     * @param destinationId
     * @param toasts
     */
    private void getToasts(Long destinationId, ArrayList toasts) {
        Destination destination = destinationService.selecOne(destinationId);
        toasts.add(destination);
        if (destination.getParent() != null) {
            getToasts(destination.getParent().getId(), toasts);
        }
    }

    @RequestMapping("detail")
    public String detail(Model model, Long id) {
        Attractions attractions = attractionsService.selectById(id);

        AttractionsDetail attractionsDetail = attractionsDetailService.selectByAttractionsId(id);

        List<Attractions> Sons = attractionsService.selectSonById(id);
        AttractionsCommentQuery qo = new AttractionsCommentQuery();
        qo.setAttractionsId(id);
        Page<AttractionsComment> page = attractionsCommentService.selectCommentForList(qo);

        model.addAttribute("page", page);

        //共享景点明细
        model.addAttribute("attractionsDetail", attractionsDetail);
        //共享景点
        model.addAttribute("attractions", attractions);
        //共享包含的景点
        model.addAttribute("Sons", Sons);

        return "attractions/detail";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    public AjaxResult commentAdd(@UserParam UserInfo userInfo, AttractionsComment comment) {
        //首先我们保存评论,需要补全信息
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setCreateTime(new Date());

        //保存评论
        attractionsCommentService.save(comment);

        //返回内容
        //增加文章评论内容,并且返回增加后的数量
        attractionsService.addReplynumByDetailId(comment.getAttractionsId());

        //Long count=strategyCommentService.selectCountByDetailId(comment.getDetailId());
        //还要增加评论数
        Integer replynum = null;
        try {
            replynum = attractionStatisRedisService.addStaticNum(comment.getAttractionsId(), 1, AttractionsStatisVO.ATTRACTIONS_STATISVO_VIEWNUM).getReplynum();

        } catch (Exception e) {
            throw new RuntimeException("网络故障");
        }
        return new AjaxResult(true, "评论成功", replynum);
    }

    @RequestMapping("/replayAdd")
    @ResponseBody
    public AjaxResult replayAdd(@UserParam UserInfo userInfo, String id, String content, Long attractionsId) {

        AttractionsComment comment = attractionsCommentService.getById(id);
        List<AttractionsComment> thumbuplist = comment.getThumbuplist();

        AttractionsComment comment2 = new AttractionsComment();
        //首先我们保存评论,需要补全信息
        comment2.setUserId(userInfo.getId());
        comment2.setUsername(userInfo.getNickname());
        comment2.setCity(userInfo.getCity());
        comment2.setLevel(userInfo.getLevel());
        comment2.setHeadUrl(userInfo.getHeadImgUrl());
        comment2.setCreateTime(new Date());
        comment2.setContent(content);
        comment2.setAttractionsId(attractionsId);

        thumbuplist.add(comment2);
        comment.setThumbuplist(thumbuplist);
        //保存评论
        attractionsCommentService.save(comment);

        return new AjaxResult(true, "评论成功");
    }
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public AjaxResult commentThumbUp(String toid,Long fromid,@UserParam UserInfo userInfo) {
        attractionsCommentService.commentThumbUp(toid,userInfo.getId());
        return new AjaxResult(true,"点赞成功");
    }
}
